/*******************************************************************************
 * Copyright (c) 2008 L.Carbonnaux.
 *******************************************************************************/

package org.lcx.taskvision.ui;

import java.net.MalformedURLException;
import java.net.URL;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

/**
 * @author Laurent Carbonnaux
 */
public class TaskVisionImages {

	private static ImageRegistry imageRegistry;

	private static final String T_EVIEW = "eview16";

	private static final String T_TOOL = "etool16";

	private static final URL baseURL = TaskVisionUiPlugin.getDefault().getBundle().getEntry("/icons/");

	public static final ImageDescriptor LOGO = create(T_EVIEW, "sv_logo_16.gif");

	public static final ImageDescriptor REFRESH = create(T_TOOL, "refresh.gif");

	public static final ImageDescriptor PRIORITY_1 = create(T_EVIEW, "priority-1.gif");

	public static final ImageDescriptor PRIORITY_2 = create(T_EVIEW, "priority-2.gif");

	public static final ImageDescriptor PRIORITY_3 = create(T_EVIEW, "priority-3.gif");

	public static final ImageDescriptor PRIORITY_4 = create(T_EVIEW, "priority-4.gif");

	public static final ImageDescriptor PRIORITY_5 = create(T_EVIEW, "priority-5.gif");
	
	public static final ImageDescriptor OVERLAY_SPRINT = create(T_EVIEW, "overlay_sprint.gif");
	public static final ImageDescriptor OVERLAY_USERSTORY = create(T_EVIEW, "overlay_userstory.gif");
	public static final ImageDescriptor OVERLAY_FEATURE = create(T_EVIEW, "overlay_feature.gif");
	public static final ImageDescriptor OVERLAY_TASK = create(T_EVIEW, "overlay_task.gif");

	private static ImageDescriptor create(String prefix, String name) {
		try {
			return ImageDescriptor.createFromURL(makeIconFileURL(prefix, name));
		} catch (MalformedURLException e) {
			return ImageDescriptor.getMissingImageDescriptor();
		}
	}

	private static URL makeIconFileURL(String prefix, String name) throws MalformedURLException {
		if (baseURL == null)
			throw new MalformedURLException();

		StringBuffer buffer = new StringBuffer(prefix);
		buffer.append('/');
		buffer.append(name);
		return new URL(baseURL, buffer.toString());
	}
	
	private static ImageRegistry getImageRegistry() {
		if (imageRegistry == null) {
			imageRegistry = new ImageRegistry();
		}

		return imageRegistry;
	}
	
	/**
	 * Lazily initializes image map.
	 */
	public static Image getImage(ImageDescriptor imageDescriptor) {
		ImageRegistry imageRegistry = getImageRegistry();
		Image image = imageRegistry.get("" + imageDescriptor.hashCode());
		if (image == null) {
			image = imageDescriptor.createImage(false);
			imageRegistry.put("" + imageDescriptor.hashCode(), image);
		}
		return image;
	}
	//TODO : complete for 3.0
	/*
	public static Image getImageForPriority(PriorityLevel priorityLevel) {
		if (priorityLevel == null) {
			return null;
		} else {
			ImageDescriptor imageDescriptor = getImageDescriptorForPriority(priorityLevel);
			if (imageDescriptor != null) {
				return ScrumVisionImages.getImage(imageDescriptor);
			}
		}
		return null;
	}

	public static ImageDescriptor getImageDescriptorForPriority(PriorityLevel priorityLevel) {
		if (priorityLevel == null) {
			return null;
		}
		switch (priorityLevel) {
		case P1:
			return ScrumVisionImages.PRIORITY_1;
		case P2:
			return ScrumVisionImages.PRIORITY_2;
		case P3:
			return ScrumVisionImages.PRIORITY_3;
		case P4:
			return ScrumVisionImages.PRIORITY_4;
		case P5:
			return ScrumVisionImages.PRIORITY_5;
		default:
			return null;
		}
	}
	*/

}
